<template>
  <div class="waypoints-panel" :class="{ collapsed: isCollapsed }">
    <!-- 标题 -->
    <div class="panel-header waypoint-header">
      <img class="left-arrow-icon" :src="leftArrowIcon" alt="左箭头" />
      <h3 class="panel-title">航点-S<img class="waypoint-icon" :src="waypointIcon" alt="航点" /></h3>
      <img class="right-arrow-icon" :src="rightArrowIcon" alt="右箭头" />
    </div>
    
    <!-- 内容 -->
    <div class="panel-content">
      <!-- 航点经纬度 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">航点经纬度</div>
        </div>
        <!-- 内容模块 -->
        <div class="content-item-content">
          <!-- 经纬度 -->
          <div class="lon-lat-container">
            <div class="lon-container">
              <el-input 
                v-model="waypoint.longitude" 
                placeholder="请输入经度"
                @input="(val) => handleNumberInput(val, 'longitude')"
              />
            </div>
            <div class="lat-container">
              <el-input 
                v-model="waypoint.latitude" 
                placeholder="请输入纬度"
                @input="(val) => handleNumberInput(val, 'latitude')"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 航点高度 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">航点高度</div>
          <!-- 选中按钮 -->
          <div class="check-btn" @click="toggleFollowSetting('altitude')" :class="{ active: followSettings.altitude }">
            <img class="check-btn-icon" :src="checkIcon" alt="选中" :class="{ unchecked: !followSettings.altitude }" />
            <div class="check-btn-text">跟随航线</div>
          </div>
        </div>
        <!-- 内容模块 -->
        <div class="content-item-content">
          <!-- 航点高度设置 -->
          <div class="waypoint-height-setting-container">
            <!-- +100 按钮 -->
            <div class="altitude-control-item clickable" @click="adjustAltitude(100)">
              <div class="control-value">+100</div>
              <div class="control-unit">m</div>
            </div>
            <!-- +10 按钮 -->
            <div class="altitude-control-item clickable" @click="adjustAltitude(10)">
              <div class="control-value">+10</div>
              <div class="control-unit">m</div>
            </div>
            <!-- 当前高度输入框 -->
            <div class="altitude-input-wrapper">
              <el-input
                v-model.number="waypoint.altitude"
                type="number"
              >
                <template #append>m</template>
              </el-input>
            </div>
            <!-- -10 按钮 -->
            <div class="altitude-control-item clickable" @click="adjustAltitude(-10)">
              <div class="control-value">-10</div>
              <div class="control-unit">m</div>
            </div>
            <!-- -100 按钮 -->
            <div class="altitude-control-item clickable" @click="adjustAltitude(-100)">
              <div class="control-value">-100</div>
              <div class="control-unit">m</div>
            </div>
          </div>
        </div>
      </div>
      <!-- 航点类型 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">航点类型</div>
          <!-- 选中按钮 -->
          <div class="check-btn" @click="toggleFollowSetting('type')" :class="{ active: followSettings.type }">
            <img class="check-btn-icon" :src="checkIcon" alt="选中" :class="{ unchecked: !followSettings.type }" />
            <div class="check-btn-text">跟随航线</div>
          </div>
        </div>
        <!-- 内容模块 -->
        <div class="content-item-content">
          <!-- 航点类型 -->
          <div class="waypoint-type-setting-container">
            <el-select v-model="waypoint.type" placeholder="请选择航点类型">
              <el-option
                v-for="item in waypointTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
      </div>
      <!-- 飞行器偏航角模式 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">飞行器偏航角模式</div>
          <!-- 选中按钮 -->
          <div class="check-btn" @click="toggleFollowSetting('yawMode')" :class="{ active: followSettings.yawMode }">
            <img class="check-btn-icon" :src="checkIcon" alt="选中" :class="{ unchecked: !followSettings.yawMode }" />
            <div class="check-btn-text">跟随航线</div>
          </div>
        </div>
        <!-- 内容模块 -->
        <div class="content-item-content">
          <!-- 飞行器偏航角模式 -->
          <div class="yaw-mode-setting-container">
            <el-select v-model="waypoint.yawMode" placeholder="请选择飞行器偏航角模式">
              <el-option
                v-for="item in yawModeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
      </div>
      <!-- 飞行速度 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">飞行速度</div>
          <!-- 选中按钮 -->
          <div class="check-btn" @click="toggleFollowSetting('speed')" :class="{ active: followSettings.speed }">
            <img class="check-btn-icon" :src="checkIcon" alt="选中" :class="{ unchecked: !followSettings.speed }" />
            <div class="check-btn-text">跟随航线</div>
          </div>
        </div>
        <!-- 内容模块 -->
        <div class="content-item-content">
          <!-- 飞行速度 -->
          <div class="waypoint-speed-setting-container">
            <!-- 滑块 -->
            <div class="slider-wrapper">
              <CommonSlider
                v-model="waypoint.speed"
                :min="0"
                :max="20"
                :step="1"
              />
            </div>
            <!-- 速度输入框 -->
            <div class="speed-input-wrapper">
              <el-input
                v-model.number="waypoint.speed"
                type="number"
              >
                <template #append>m/s</template>
              </el-input>
            </div>
          </div>
        </div>
      </div>
      <!-- 航点动作 -->
      <div class="content-item">
        <!-- 标题模块 -->
        <div class="content-item-title">
          <div class="content-item-title-text">航点动作</div>
          <!-- 普通按钮 -->
          <div class="normal-btn">
            <img class="normal-icon" :src="addActionIcon" alt="添加动作" />
            <div class="normal-text">添加动作</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 收起按钮 -->
    <div class="fold-btn" @click="toggleCollapse">
      <img class="fold-btn-bg" :src="foldBg" alt="右收起背景" />
      <img class="fold-btn-to-right-icon" :class="{ rotated: isCollapsed }" :src="foldIconToRight" alt="向右收起" />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import CommonSlider from '@/components/CommonSlider.vue'

// 航点图标
import waypointIcon from '@/assets/icons/icon_waypoint_marker.png'
// 左箭头
import leftArrowIcon from '@/assets/icons/icon_left_arrow.png'
// 右箭头
import rightArrowIcon from '@/assets/icons/icon_right_arrow.png'
// 选中
import checkIcon from '@/assets/icons/icon_checked_btn.png'
// 添加动作
import addActionIcon from '@/assets/icons/icon_add_action.png'
// 收起icon
import foldBg from '@/assets/icons/icon_fold_right_bg.png' // 收起按钮的背景
import foldIconToRight from '@/assets/icons/icon_fold_to_right_arrow.png' // 向右icon

// 控制面板折叠状态
const isCollapsed = ref(false)

// 切换折叠状态
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

// 航点数据
const waypoint = ref({
  longitude: '', // 经度
  latitude: '', // 纬度
  altitude: 180, // 高度
  type: '', // 航点类型
  yawMode: '', // 飞行器偏航角模式
  speed: 10 // 飞行速度
})

// 跟随航线状态
const followSettings = ref({
  altitude: false, // 航点高度跟随航线
  type: false, // 航点类型跟随航线
  yawMode: false, // 飞行器偏航角模式跟随航线
  speed: false // 飞行速度跟随航线
})

/**
 * 切换跟随航线状态
 * @param {string} setting - 设置项名称
 */
const toggleFollowSetting = (setting) => {
  followSettings.value[setting] = !followSettings.value[setting]
}

// 航点类型选项
const waypointTypeOptions = [
  { value: 'normal', label: '普通航点' },
  { value: 'hover', label: '悬停航点' },
  { value: 'curved', label: '曲线航点' },
  { value: 'panorama', label: '全景航点' },
  { value: 'mapping', label: '测绘航点' }
]

// 飞行器偏航角模式选项
const yawModeOptions = [
  { value: 'auto', label: '自动' },
  { value: 'manual', label: '手动' },
  { value: 'follow_route', label: '沿航线方向' },
  { value: 'toward_poi', label: '朝向兴趣点' },
  { value: 'fixed', label: '固定角度' }
]

/**
 * 限制输入只能是数字和小数点
 * @param {string} value - 输入的值
 * @param {'longitude' | 'latitude'} field - 要更新的字段名
 */
const handleNumberInput = (value, field) => {
  // 移除非数字和非小数点的字符
  let newValue = value.replace(/[^\d.]/g, '')
  
  // 确保只有一个小数点
  const parts = newValue.split('.')
  if (parts.length > 2) {
    newValue = parts[0] + '.' + parts.slice(1).join('')
  }
  
  // 更新对应字段的值
  waypoint.value[field] = newValue
}

/**
 * 调整航点高度
 * @param {number} value - 要增加或减少的值
 */
const adjustAltitude = (value) => {
  waypoint.value.altitude += value
  // 限制最小值为0
  if (waypoint.value.altitude < 0) {
    waypoint.value.altitude = 0
  }
}
</script>

<style lang="scss" scoped>
@import '@/style/element-overrides.scss';
.waypoints-panel {
  position: relative;
  width: 510px;
  height: 903px;
  background: url('@/assets/images/edit_airline_bg.png') no-repeat center center;
  background-size: 100% 100%;
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
  align-items: flex-start;
  transition: transform 0.3s ease, width 0.3s ease;
  
  // 折叠状态
  &.collapsed {
    transform: translateX(541px); // 向右收起，只露出收起按钮部分
    
    .panel-header,
    .panel-content {
      opacity: 0;
      pointer-events: none;
    }
  }
  
  .panel-header {
    width: 100%;
    height: 38px;
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    box-sizing: border-box;
    transition: opacity 0.3s ease;
    // 航点头部
    &.waypoint-header {
      justify-content: space-between;
      padding: 0 20px;
      .left-arrow-icon {
        width: 7px;
        height: 12px;
        cursor: pointer;
      }
      .right-arrow-icon {
        width: 7px;
        height: 12px;
        cursor: pointer;
      }
    }
    
    .panel-title {
      font-size: 18px;
      font-weight: 900;
      letter-spacing: 2px;
      line-height: 18px;
      background: linear-gradient(180deg, rgba(238, 238, 238, 1) 0%, rgba(238, 238, 238, 1) 40%, rgba(59, 135, 202, 1) 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      filter: drop-shadow(0px 2px 3px rgba(0, 0, 0, 0.5));
      display: flex;
      align-items: center;
      gap: 5px;
      
      .waypoint-icon {
        width: auto;
        height: 16px;
      }
    }
  }
  
  .panel-content {
    flex: 1;
    width: 100%;
    max-height: 865px;
    overflow-y: auto;
    padding: 30px 16px;
    transition: opacity 0.3s ease;
    
    // 滚动条样式
    &::-webkit-scrollbar {
      width: 5px;
    }
    
    &::-webkit-scrollbar-track {
      background: transparent;
      border-radius: 2.5px;
    }
    
    &::-webkit-scrollbar-thumb {
      background: rgba(255, 255, 255, 0.3);
      border-radius: 2.5px;
      transition: all 0.3s;
      
      &:hover {
        background: rgba(255, 255, 255, 0.5);
      }
    }
    
    // Firefox 滚动条样式
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
    
    // 背景卡片
    .bg-card {
      background: rgba(2, 92, 109, 0.3);
      backdrop-filter: blur(6px);
      padding: 16px;
      box-sizing: border-box;
      
      & + .bg-card {
        margin-top: 8px;
      }
    }

    // 内容项
    .content-item {
      & + .content-item {
        margin-top: 16px;
      }
      .content-item-title {
        display: flex;
        flex-flow: row nowrap;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
        .content-item-title-text {
          font-size: 14px;
          font-weight: 400;
          letter-spacing: 0px;
          line-height: 14px;
          color: rgba(255, 255, 255, 0.7);
        }
        // 选中按钮
        .check-btn {
          display: flex;
          flex-flow: row nowrap;
          justify-content: flex-start;
          align-items: center;
          cursor: pointer;
          transition: all 0.3s;
          
          &:hover {
            .check-btn-text {
              color: rgba(255, 255, 255, 0.9);
            }
          }
          
          .check-btn-icon {
            width: 12px;
            height: 12px;
            margin-right: 5px;
            transition: opacity 0.3s;
            
            &.unchecked {
              opacity: 0.3;
            }
          }
          .check-btn-text {
            font-size: 14px;
            font-weight: 400;
            letter-spacing: 0px;
            line-height: 14px;
            color: rgba(255, 255, 255, 0.7);
            transition: color 0.3s;
          }
        }
        // 普通按钮
        .normal-btn {
          display: flex;
          flex-flow: row nowrap;
          justify-content: center;
          align-items: center;
          width: 146px;
          height: 32px;
          border-radius: 4px;
          background: linear-gradient(180deg, rgba(25, 167, 181, 1) 0%, rgba(2, 48, 60, 1) 53.09%, rgba(25, 167, 181, 1) 100%);
          border: 2px solid rgba(20, 109, 142, 1);
          cursor: pointer;
          .normal-icon {
            width: 14px;
            height: 14px;
            margin-right: 8px;
          }
          .normal-text {
            font-size: 14px;
            font-weight: 400;
            letter-spacing: 0px;
            line-height: 14px;
            color: rgba(255, 255, 255, 1);
          }
        }
      }
    }

    // 航点经纬度
    .lon-lat-container {
      display: flex;
      flex-flow: row nowrap;
      justify-content: space-between;
      align-items: center;
      .lon-container {
        width: 235px;
      }
      .lat-container {
        width: 235px;
      }
    }

    // 航点高度
    .waypoint-height-setting-container {
      display: flex;
      flex-flow: row nowrap;
      justify-content: space-between;
      align-items: center;
      // gap: 0;
      
      // 高度控制项通用样式
      .altitude-control-item {
        width: 89px;
        height: 32px;
        border: 1px solid rgba(175, 215, 230, 0.5);
        background: transparent;
        display: flex;
        align-items: center;
        transition: all 0.3s;
        box-shadow: none;
        
        // 可点击的按钮样式
        &.clickable {
          cursor: pointer;
          
          &:hover {
            background: rgba(0, 255, 244, 0.05);
            z-index: 1; // 确保hover时边框显示在上层
          }
          
          &:active {
            background: rgba(0, 255, 244, 0.1);
          }
        }
        
        // 控制值
        .control-value {
          flex: 1;
          text-align: center;
          font-size: 14px;
          color: rgba(0, 255, 244, 1);
          font-weight: 500;
        }
        
        // 单位
        .control-unit {
          padding: 0 10px;
          font-size: 14px;
          color: rgba(0, 255, 244, 1);
          border-left: 1px solid rgba(175, 215, 230, 0.5);
          height: 30px;
          line-height: 30px;
        }
      }
      
      // 高度输入框容器
      .altitude-input-wrapper {
        width: 89px;
        
        :deep(.el-input) {
          height: 32px !important;
          
          .el-input__wrapper {
            height: 32px !important;
            border-radius: 0 !important;
            border-left: none !important;
            border-right: none !important;
          }
          
          .el-input__inner {
            height: 30px !important;
            line-height: 30px !important;
            text-align: center !important;
          }
          
          .el-input-group__append {
            height: 30px !important;
            line-height: 30px !important;
            border-radius: 0 !important;
          }
        }
        
        // 隐藏数字输入框的上下箭头
        :deep(.el-input__inner) {
          &::-webkit-inner-spin-button,
          &::-webkit-outer-spin-button {
            -webkit-appearance: none;
            appearance: none;
            margin: 0;
          }
          &[type=number] {
            -moz-appearance: textfield;
            appearance: textfield;
          }
        }
      }
    }

    // 航点类型
    .waypoint-type-setting-container {
      width: 100%;
      padding: 8px;
      box-sizing: border-box;
      background: rgba(2, 92, 109, 0.3);
      backdrop-filter: blur(6px);
    }

    // 飞行器偏航角模式
    .yaw-mode-setting-container {
      width: 100%;
      padding: 8px;
      box-sizing: border-box;
      background: rgba(2, 92, 109, 0.3);
      backdrop-filter: blur(6px);
    }

    // 飞行速度
    .waypoint-speed-setting-container {
      width: 100%;
      padding: 8px;
      box-sizing: border-box;
      background: rgba(2, 92, 109, 0.3);
      backdrop-filter: blur(6px);
      display: flex;
      flex-flow: row nowrap;
      justify-content: space-between;
      align-items: center;
      gap: 14px;
      
      // 滑块容器
      .slider-wrapper {
        flex: 1;
      }
      
      // 速度输入框容器
      .speed-input-wrapper {
        width: 120px;
        margin-left: 8px;
        
        // 隐藏数字输入框的上下箭头
        :deep(.el-input__inner) {
          &::-webkit-inner-spin-button,
          &::-webkit-outer-spin-button {
            -webkit-appearance: none;
            appearance: none;
            margin: 0;
          }
          &[type=number] {
            -moz-appearance: textfield;
            appearance: textfield;
          }
        }
      }
    }
  }

  // 收起按钮
  .fold-btn {
    position: absolute;
    top: 50%;
    left: -26px;
    z-index: 2;
    transform: translateY(-50%);
    width: 26px;
    height: 81px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      .fold-btn-to-right-icon {
        transform: translate(-50%, -50%) scale(1.2);
        
        &.rotated {
          transform: translate(-50%, -50%) rotate(180deg) scale(1.2);
        }
      }
    }
    .fold-btn-bg {
      width: 100%;
      height: 100%;
    }
    .fold-btn-to-right-icon {
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 2;
      transform: translate(-50%, -50%);
      width: 14px;
      height: 14px;
      transition: transform 0.3s ease;
      
      // 折叠状态时旋转180度
      &.rotated {
        transform: translate(-50%, -50%) rotate(180deg);
      }
    }
  }
}
</style>